#!/bin/bash -eu
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
################################################################################

echo -n "Precompiling centipede"

# Build Centipede with bazel.
cd "$SRC/fuzztest/centipede/"
apt-get update && apt-get install libssl-dev -y
unset CXXFLAGS CFLAGS
# We need to use an older version of BAZEL because fuzztest relies on WORKSPACE
# Ref: https://github.com/google/oss-fuzz/pull/12838#issue-2733821058
export USE_BAZEL_VERSION=7.4.0
echo 'build --cxxopt=-stdlib=libc++ --linkopt=-lc++' >> /tmp/centipede.bazelrc
bazel --bazelrc=/tmp/centipede.bazelrc build -c opt :all
unset USE_BAZEL_VERSION

# Prepare the weak symbols:
# This is necessary because we compile the target binary and the intermediate
# auxiliary binaries with the same cflags. The auxiliary binaries do not need
# data-flow tracing flags, but will still throw errors when they cannot find
# the corresponding functions.
# The weak symbols provides fake implementations for intermediate binaries.
$CXX "$SRC/fuzztest/centipede/weak_sancov_stubs.cc" -c -o "$SRC/fuzztest/centipede/weak.o"

echo 'Removing extra stuff leftover to avoid bloating image.'

rm -rf /clang-*.tgz /clang

BAZEL_BIN_REAL_DIR=$(readlink -f $CENTIPEDE_BIN_DIR)
rm -rf $CENTIPEDE_BIN_DIR
mkdir -p $CENTIPEDE_BIN_DIR
mv $BAZEL_BIN_REAL_DIR/centipede/{centipede,libcentipede_runner.pic.a} $CENTIPEDE_BIN_DIR
rm -rf /root/.cache

echo 'Done.'
